package cn.edu.sgu.www.mhxysy.controller.qiling;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.qiling.QilingAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingAttribute;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingAttributeService;
import cn.edu.sgu.www.mhxysy.vo.except.AttributeVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "器灵属性管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/qiling_attribute", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class QilingAttributeController {

	private final QilingAttributeService qilingAttributeService;

	@Autowired
	public QilingAttributeController(QilingAttributeService qilingAttributeService) {
		this.qilingAttributeService = qilingAttributeService;
	}

	@ApiOperation("一键分配器灵属性")
	@RequestMapping(value = "/allocate", method = RequestMethod.POST)
	public JsonResult<Void> allocate(
			@ApiParam(name = "qilingId", value = "器灵ID", required = true)
			@NotBlank(message = CustomMessages.QILING_ID_NOT_NULL) String qilingId) {
		qilingAttributeService.allocate(qilingId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("添加器灵属性")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated QilingAttributeDTO qilingAttributeDTO) {
		qilingAttributeService.insert(qilingAttributeDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除器灵属性")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "器灵属性ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		qilingAttributeService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改器灵属性")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated QilingAttributeDTO qilingAttributeDTO) {
		qilingAttributeService.updateById(qilingAttributeDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询器灵属性")
	@RequestMapping(value = "/selectAttributes", method = RequestMethod.GET)
	public JsonResult<List<AttributeVO>> selectAttributes(
			@ApiParam(name = "qilingId", value = "器灵ID", required = true)
			@NotBlank(message = CustomMessages.QILING_ID_NOT_NULL) String qilingId) {
		List<AttributeVO> list = qilingAttributeService.selectAttributes(qilingId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过器灵ID查询器灵属性列表")
	@RequestMapping(value = "/selectByQilingId", method = RequestMethod.GET)
	public JsonResult<List<QilingAttribute>> selectByQilingId(
			@ApiParam(name = "qilingId", value = "器灵ID", required = false)
			@RequestParam(required = false) String qilingId) {
		List<QilingAttribute> list = qilingAttributeService.selectByQilingId(qilingId);

		return JsonResult.success(null, list);
	}

}